import {defineStore} from 'pinia'

export const useStore = defineStore('user', {
    state:()=>({
        // 所有这些属性都将自动推断其类型
        u_user_info: {}||uni.getStorageSync("u_user_info"),
        token:""||uni.getStorageSync("token")
    }),
    getters: {
        getUserInfo: (state) => {
            if (state.u_user_info['intro']){
                return state.u_user_info
            }else {
                return uni.getStorageSync("u_user_info")
            }
        },
        getToken:(state)=>{
            if (state.token!==""){
                return state.token
            }else {
                return uni.getStorageSync("token")
            }
        }
    },
    actions:{
        saveUserInfo(userInfo){
            //持久化操作
            uni.setStorageSync("u_user_info",userInfo)
            this.u_user_info = userInfo
        },
        saveToken(token){
            //持久化操作
            uni.setStorageSync("token",token)
            console.warn("保存token成功",uni.getStorageSync("token"))
            this.token = token
        },
        clearUserInfo(){
            uni.removeStorageSync("token")
            uni.removeStorageSync("u_user_info")
            this.u_user_info={}
        }
    },
    persist: {
        enabled: true
    },

})